﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile.Nodes
{
	internal class Throw : Executable
	{
		// Nullable. If null, rethrow the caught exception.
		public Expression Expression { get; private set; }
		public Token Token { get; private set; }

		private Throw(Expression expression, Token token)
		{
			this.Expression = expression;
			this.Token = token;
		}

		public static Throw ParseThrow(Tokens tokens)
		{
			Token token = tokens.PopExpected("throw");
			if (tokens.PopIfPresent(";"))
			{
				return new Throw(null, token);
			}

			Expression expression = Expression.Parse(tokens);
			tokens.PopExpected(";");
			return new Throw(expression, token);
		}

		public override void ResolveCode(Class c, Dictionary<object, object> parents)
		{
			this.Expression.ResolveCode(c, parents);
		}

		public override void ResolveClassToThis(Class c)
		{
			this.Expression.ResolveClassToThis(c);
		}
	}
}
